/*
 * Funambol is a mobile platform developed by Funambol, Inc.
 * Copyright (C) 2008 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#ifndef INCL_CONTACT_LISTENER
#define INCL_CONTACT_LISTENER

using namespace Funambol;


/**
 * Listeners for the iPhone client, wrapping the C++ calls to ObjeC listeners.
 */ 
class iPhoneSyncSourceListener : public SyncSourceListener {
    
private:
    
    id uiController;
    NSString* syncSourceName;
    bool receiving;
    
public:
    
    ~iPhoneSyncSourceListener(){}
    iPhoneSyncSourceListener() {}
    iPhoneSyncSourceListener(id controller, const char* ssn) : uiController(controller) {
        receiving = false;
        syncSourceName = [[NSString alloc] initWithCString:ssn];
        
    }
    
    // listen for the Sync Begin Event
    void syncSourceBegin(SyncSourceEvent& event) {
        // Do not propagate as we synthetize this event as early as possible
        // in the SyncThread below
        LOG.debug("syncSourceBegin");
    }
    
    // listen for the Sync End Event
    void syncSourceEnd(SyncSourceEvent& event) {
        // Do not propagate as we synthetize this event as late as possible
        // in the SyncThread below
        LOG.debug("syncSourceEnd");
        //[uiController endSync:event.getData()];
        [uiController sendFinishing: syncSourceName isReceiving:receiving];
        if(receiving){
            receiving = false;
        }else {
            receiving = true;
        }


    }
    
    // listen for the SyncMode requested by the server
    void syncSourceSyncModeRequested(SyncSourceEvent& event) {
        LOG.debug("syncSourceSyncModeRequested");
    }
    
    // listen for total client items (number of changes) sent by Client.
    void syncSourceTotalClientItems(SyncSourceEvent& event) {
        LOG.debug("syncSourceTotalClientItems");
        [uiController sendingClientItems:syncSourceName num:event.getData()];
    }
    
    // listen for total server items (number of changes) sent by Server.
    void syncSourceTotalServerItems(SyncSourceEvent& event) {
        LOG.debug("syncSourceTotalServerItems");
        [uiController receivingServerItems:syncSourceName num: event.getData()];
    }
    
};

/**
 * Listeners for the iPhone client, wrapping the C++ calls to ObjeC listeners.
 */ 
class iPhoneSyncItemListener : public SyncItemListener {
    
private:
    
    id uiController;    
    NSString* syncSourceName;
    
public:
    ~iPhoneSyncItemListener(){}
    iPhoneSyncItemListener() {}
    iPhoneSyncItemListener(id controller, const char* ssn) : uiController(controller) {
        syncSourceName = [[NSString alloc] initWithCString:ssn];
    }
    
    // We cannot have virtual methods in this class
    void itemAddedByServer(SyncItemEvent& event) {
        [uiController receivedAddItem:syncSourceName];
        LOG.debug("item added by server");
    }
    
    void itemDeletedByServer(SyncItemEvent& event) {
        [uiController receivedDeleteItem:syncSourceName];
        LOG.debug("itemDeletedByServer");
    }
    
    void itemUpdatedByServer(SyncItemEvent& event) {
        [uiController receivedUpdateItem:syncSourceName];
        LOG.debug("itemUpdatedByServer");
    }
    
    void itemAddedByClient(SyncItemEvent& event) {
        [uiController sentAddItem:syncSourceName];
        LOG.debug("itemAddedByClient");
    }
    
    void itemDeletedByClient(SyncItemEvent& event) {
        [uiController sentDeleteItem:syncSourceName];
        LOG.debug("itemDeletedByClient");
    }
    
    void itemUpdatedByClient(SyncItemEvent& event) {
        [uiController sentUpdateItem:syncSourceName];
        LOG.debug("itemUpdatedByClient");
    }
};
#endif
